<template>
    <!-- 列表页面 -->
    <div class="tableSample" v-if="!showAdd && !showEdit">
        <div class="header">
            <div class="header-left">
                <p class="title">订单信息
                </p>
            </div>
            <div class="header-right">
                <lin-search @query="onQueryChange" placeholder="请输入姓名"/>
                <lin-search @query="onQueryChangeOne" placeholder="请输入电话" style="margin-left: 10px;"/>
                <lin-search @query="onQueryChangeTwo" placeholder="请输入身份证号" style="margin-left: 10px;"/>
            </div>
            <el-select  size="medium" filterable v-model="search.status" @change="handleChangeType" placeholder="请选择状态" clearable>
                <el-option
                        v-for="(type, index) in statusArr"
                        :key="index"
                        :label="type.name"
                        :value="type.id">
                </el-option>
            </el-select>
            <el-select  size="medium" filterable v-model="lin_user_id" @change="handleChangeAdmin" placeholder="请选择酒店后台" clearable v-if="isSuper">
                <el-option
                        v-for="(admin, index) in adminArr"
                        :key="index"
                        :label="admin.username"
                        :value="admin.id">
                </el-option>
            </el-select>
        </div>
        <!-- 表格 -->
        <el-table
                :data="tableData"
                stripe
                border
                v-loading="loading"
        >
            <el-table-column type="expand">
                <template slot-scope="props">
                    <el-form label-position="left" inline class="demo-table-expand">
                        <el-form-item label="退款描述" v-if="props.row.refund_imgs">
                            <span>{{ props.row.refund_remark }}</span>
                        </el-form-item>
                        <el-form-item label="退款图片说明" v-if="props.row.refund_imgs.length > 0">
                            <span v-for="(item,index) in props.row.refund_imgs" :key="index">
                                <img :src="item.url" width="100px" style="margin: 10px" />
                            </span>
                        </el-form-item>
                    </el-form>
                </template>
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="order_no"
                    label="订单号">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="hotel_room_name"
                    label="房间名称">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="start_date"
                    label="开始时间">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="end_date"
                    label="结束时间">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="estimate_time"
                    label="预计到店时间">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="name"
                    label="订房人姓名">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="phone"
                    label="订房人电话">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="id_no"
                    label="订房人身份证号">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="price"
                    label="订单价格">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="admin_user.username"
                    label="后台用户名">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="status_name"
                    label="订单状态">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="pay_time_name"
                    label="支付时间">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="refund_time_name"
                    label="申请退款时间">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="send_time_name"
                    label="完成时间">
            </el-table-column>
            <!-- 操作列 -->
            <el-table-column label="操作" fixed="right" width="180" >
                <template slot-scope="scope">
                    <el-button
                            type="primary"
                            plain
                            size="mini"
                            @click.native.prevent.stop="handleEditOne(scope)"
                            v-if="scope.row.status === 2 && !isSuper"
                    >入住
                    </el-button>
                    <el-button
                            type="primary"
                            plain
                            size="mini"
                            @click.native.prevent.stop="handleEditTwo(scope)"
                            v-if="scope.row.status === 4 && !isSuper"
                    >退房
                    </el-button>
                    <el-button
                            type="primary"
                            plain
                            size="mini"
                            @click.native.prevent.stop="handleEditThree(scope)"
                            v-if="scope.row.status === 3 && isSuper"
                    >同意退款
                    </el-button>
                    <el-button
                            type="primary"
                            plain
                            size="mini"
                            @click.native.prevent.stop="handleEditThree(scope)"
                            v-if="scope.row.status === 5 && isSuper"
                    >同意退房
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="pagination">
            <el-pagination
                    @current-change="handleCurrentChange"
                    :background="true"
                    :page-size="pageCount"
                    :current-page="currentPage"
                    v-if="refreshPagination"
                    layout="total, prev, pager, next, jumper"
                    :total="total_nums"
            ></el-pagination>
        </div>
    </div>
    <room-order-edit v-else-if="showEdit" :linUserId="lin_user_id" :roomOrderId="roomOrderId"   @editClose="editClose"></room-order-edit>
</template>
<script>
import room from '@/models/room'
import admin from '@/models/admin'
import LinSearch from '@/components/base/search/lin-search'
import User from '@/lin/models/user'
import RoomOrderEdit from './RoomOrderEdit'

export default {
  components: {
    LinSearch,
    RoomOrderEdit,
  },
  data() {
    return {
      statusArr: [
        { id: 2, name: '已订房' },
        { id: 3, name: '未入住申请退款' },
        { id: 6, name: '未入住已退款' },
        { id: 4, name: '已入住' },
        { id: 5, name: '已入住未退款' },
        { id: 7, name: '已退房' },
      ],
      tableData: [],
      dialogVisible: false,
      loading: false,
      showAdd: false,
      showEdit: false,
      isSuper: false,
      lin_user_id: '',
      roomOrderId: '',
      search: {
        name: '',
        phone: '',
        id_no: '',
        status: '',
      },
      searchAdmin: '',
      adminArr: [],
      // 分页相关
      refreshPagination: true, // 页数增加的时候，因为缓存的缘故，需要刷新Pagination组件
      currentPage: 1, // 默认获取第一页的数据
      pageCount: 10, // 每页10条数据
      total_nums: 0, // 分组内的总数
    }
  },
  computed: {},
  created() {
    this.getAdminUser()
    this.getAdminAll()
  },
  methods: {
    handleEditOne(val) {
      this
        .$confirm('用户已入住酒店?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
        .then(async () => {
          this.loading = true
          const res = await room.RoomOrderCheck({ order_id: val.row.id, lin_user_id: this.lin_user_id })
          if (res.error_code === 0) {
            this.$message.success(`${res.msg}`)
          }
          this._getTableData(this.currentPage, this.pageCount)
        })
    },
    handleEditTwo(val) {
      this.showEdit = true
      this.roomOrderId = val.row.id
    },
    handleEditThree(val) {
      this.roomOrderId = val.row.id
      this.$prompt('请输入退款金额', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(async ({ value }) => {
        this.loading = true
        const res = await room.RoomOrderSendOne({
          order_id: val.row.id,
          lin_user_id: this.lin_user_id,
          price: value,
        })
        if (res.error_code === 0) {
          this.$message.success(`${res.msg}`)
        }
        this._getTableData(this.currentPage, this.pageCount)
      })
    },
    handleClose(done) {
      done()
    },
    editClose() {
      this.showEdit = false
      this.getAdminUser()
      this.getAdminAll()
    },
    async getAdminAll() {
      this.adminArr = await admin.getAdminAll()
    },
    async getAdminUser() {
      this.loading = true
      const res = await User.getInformation()
      this.isSuper = res.isSuper
      if (res.linUserId > 1) {
        this.lin_user_id = res.linUserId
      }
      // 获取数据
      this._getTableData(this.currentPage, this.pageCount)
    },
    // 获取数据
    async _getTableData(start, count) {
      const res = await room.getRoomOrderList(start, count, {
        name: this.search.name,
        phone: this.search.phone,
        id_no: this.search.id_no,
        lin_user_id: this.lin_user_id,
        status: this.search.status,
      })
      this.tableData = res.data
      this.total_nums = res.count
      this.$nextTick(() => {
        this.loading = false
      })
    },
    // 修改首页推荐
    async submitForm() {
      const self = this
      // eslint-disable-next-line no-unused-vars
      const info = {
        id: self.roomId,
        recommend: self.editForm.recommend,
        order_sort: self.editForm.order_sort,
      }
      try {
        // eslint-disable-next-line no-unused-vars
        const res = await room.updateRoomOne(info)
        if (res.error_code === 0) {
          self.$message.success(`${res.msg}`)
          self.loading = true
          self._getTableData(self.currentPage, self.pageCount)
          self.dialogVisible = false
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 改变管理员
    handleChangeAdmin() {
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    handleAdd() {
      if (this.tableData.length > 0) {
        this.orderSort = this.tableData[0].order_sort + 1
      }
      this.showAdd = true
    },
    // 切换分页
    async handleCurrentChange(val) {
      this.currentPage = val
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    handleEdit(val) {
      this.showEdit = true
      this.roomOrderId = Number(val.row.id)
    },
    // 搜索姓名
    onQueryChange(query) {
      this.search.name = query.trim()
      if (!query) {
        this._getTableData(1, 10)
        return
      }
      this.loading = true
      this.currentPage = 1
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
    // 搜索姓名
    onQueryChangeOne(query) {
      this.search.phone = query.trim()
      if (!query) {
        this._getTableData(1, 10)
        return
      }
      this.loading = true
      this.currentPage = 1
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
    // 搜索姓名
    handleChangeType() {
      this.loading = true
      this.currentPage = 1
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
    // 搜索姓名
    onQueryChangeTwo(query) {
      this.search.id_no = query.trim()
      if (!query) {
        this._getTableData(1, 10)
        return
      }
      this.loading = true
      this.currentPage = 1
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
  },
}
</script>

<style lang="scss" scoped>
    .tableSample {
        padding: 0 0 30px;

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 30px;
            border-bottom: 1px solid #dae1ed;

            .header-left {
                float: left;

                .title {
                    height: 59px;
                    line-height: 59px;
                    color: $parent-title-color;
                    font-size: 16px;
                    font-weight: 500;
                }
            }

            .header-right {
                float: right;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
        }

        .table-main {
            padding: 0 30px;
            margin-top: 30px;
        }

        .top-operate {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            margin-top: 30px;
        }

        .sort-input {
            width: 50px;
            background: none;
            justify-content: space-around;
        }

        .table-edit {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100%;
            .content {
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }

            .cell-icon {
                cursor: pointer;
                color: #3963bc;
                font-size: 16px;
            }

            .cell-icon-edit {
                display: flex;
                margin-left: 20px;
                width: 50px;
                justify-content: space-between;

                .cell-cancel {
                    cursor: pointer;
                    color: #3963bc;
                    font-size: 16px;
                }

                .cell-save {
                    cursor: pointer;
                    color: #3963bc;
                    font-size: 16px;
                    margin-right: -20px;
                }
            }
        }

        .m-20 {
            margin-bottom: 10px;
            margin-top: 5px;
        }

        .summary {
            display: flex;
            justify-content: flex-start;
            flex-direction: row;

            img {
                width: 135px;
                height: 200px;
            }

            .demo-table-expand {
                font-size: 0;
                margin-left: 30px;
                display: flex;
                flex-direction: column;

                label {
                    width: 90px;
                    color: #99a9bf;
                }

                .el-form-item {
                    margin-right: 0;
                    margin-bottom: 0;
                    display: flex;
                    flex-direction: row;
                }
            }
        }

        .pagination {
            display: flex;
            justify-content: flex-end;
            margin: 20px;
        }
    }
    // dialog
    .tableSample /deep/ .el-dialog__footer {
        text-align: left;
        padding-left: 30px;
    }

    .tableSample /deep/ .el-dialog__header {
        padding-left: 30px;
    }

    .tableSample /deep/ .el-dialog__body {
        padding: 30px;
    }

    .demo-table-expand {
        font-size: 0;
    }
    .demo-table-expand label {
        width: 90px;
        color: #99a9bf;
    }
    .demo-table-expand .el-form-item {
        margin-right: 0;
        margin-bottom: 0;
        width: 50%;
    }
</style>
